<template>
    <div class="header">
        <!-- 下载列表 -->
        <el-dialog
        title="下载列表"
        custom-class="examine-style"
        v-model="downList"
        width="80%"
        >
        <div class="cardTits">
            <el-table :data="tableData" ref="multipleTable" stripe style="width: 100%">
            <el-table-column prop="fileName" label="文件名称">
            </el-table-column>
            <el-table-column prop="status" label="状态" width="120">
                <template #default="item">
                {{ status[item.row.status] }}
                </template>
            </el-table-column>
            <el-table-column prop="fileSize" label="大小" width="120">
                <template #default="item">
                    <template v-if="item.row.fileSize == null">--</template>
                    <template v-else>{{item.row.fileSize}}</template>
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间">
                <template #default="item">
                  {{ format('yyyy-MM-dd hh:mm:ss', new Date(item.row.createTime)) }}
                  </template>
            </el-table-column>
            <el-table-column prop="failReason" label="失败原因">
                <template #default="item">
                    <template v-if="item.row.failReason == null">--</template>
                    <template v-else>{{item.row.failReason}}</template>
                </template>
            </el-table-column>
            <el-table-column prop="downloadNum" label="下载次数" width="120">
            </el-table-column>
            <el-table-column
                prop="caozuo"
                label="操作"
                width="170"
                class-name="editor"
            >
                <template #default="item">
                    <el-button v-if="item.row.status == 2" type="text" @click="retryDownLoad(item.row.id)">重新生成</el-button>
                    <el-button @click="deleteDowloadInfo(item.row.id)" type="text">删除</el-button>
                    <el-button v-if="item.row.status == 1" type="text" @click="openUrl(item.row.successUrl,item.row.id)">下载</el-button>
                </template>
            </el-table-column>
            </el-table>
            <el-pagination
            v-if="tableData.length > 0"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="arrPage"
            :page-size="selectPage"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            >
            </el-pagination>
        </div>
        </el-dialog>
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <i v-if="!collapse" class="el-icon-s-fold"></i>
            <i v-else class="el-icon-s-unfold"></i>
        </div>
        <div class="logo">合规云</div>
        <div class="header-right">
            <div class="header-user-con">
                <!-- 消息中心 -->
                <div class="btn-bell" @click="downListShow">
                    <img src="../assets/img/icon_banli.png">
                    <b class="gtgsh">下载列表</b>
                </div>
                <!-- 用户头像 -->
                <div class="user-avator">
                    <img src="../assets/img/img.jpg" />
                </div>
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{username}}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <!-- <a href="https://github.com/lin-xin/vue-manage-system" target="_blank">
                                <el-dropdown-item>项目仓库</el-dropdown-item>
                            </a> -->
                            <!-- <a :href="'http://rule.inspursmartcity.com:8080/ucenter/index/?' + hegui_token" target="_blank">
                                <el-dropdown-item>用户中心</el-dropdown-item>
                            </a> -->
                            <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            currentPage: 1,
            total: 0, // 总条数
            arrPage: [10, 20], // 每页条数
            selectPage: 10, // 每页条数
            downList: false,
            fullscreen: false,
            name: "linxin",
            message: 2,
            tableData:[],
            hegui_token: '',
            status: {
                0: '生成中',
                1: '已生成',
                2: '生成失败',
            },
        };
    },
    computed: {
        username() {
            let username = localStorage.getItem("hegui_username");
            return username ? username : this.name;
        },
        collapse() {
            return this.$store.state.collapse;
        }
    },
    created(){
        // this.hegui_token = '&&businessType=' + 1 + '&&platformId=' + 2 + '&&platformNo=' + 3 + '&&businessNo=' + 4
        this.hegui_token = '&&Authorization=' + localStorage.getItem("hegui_token");
    },
    methods: {
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == "loginout") {
                this.$http({
                    url: '/api/consumer/doLogout',
                    method: 'post',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }).then(data => {
                    if(data.code === 0) {
                        this.$message.success("已退出登录");
                        this.$router.push("/login");
                        localStorage.removeItem("hegui_token");
                        localStorage.removeItem("hegui_businessid");
                        localStorage.removeItem("hegui_username");
                    }
                })
            }
        },
        retryDownLoad(id){
            this.$http({
                url: "/api/download/task/retryDownLoad?id=" + id,
                method: "post",
                headers: {
                "Content-Type": "application/json",
                },
            }).then((data) => {
                if (data.code === 0) {
                    // this.$message.success("重新下载成功");
                    this.downListShow()
                }
            });
        },
        deleteDowloadInfo(id){
            this.$http({
                url: "/api/download/task/deleteDowloadInfo?id=" + id,
                method: "post",
                headers: {
                "Content-Type": "application/json",
                },
            }).then((data) => {
                if (data.code === 0) {
                    this.$message.success("删除成功");
                    this.downListShow()
                }
            });
        },
        downListShow(){
            this.downList = true;
            let loginUserName = localStorage.getItem("hegui_username");
            this.$http({
                url: "/api/download/task/selectList",
                method: "post",
                headers: {
                "Content-Type": "application/json",
                },
                data: {
                    pageNum: this.currentPage,
                    pageSize: this.selectPage,
                    loginUserName: loginUserName,
                    fileRes: 10001,
                },
            }).then((data) => {
                if (data.code === 0) {
                    this.tableData = data.result.list;
                    this.total = data.result.total;
                }
            });
        },
        handleSizeChange(val) {
        // 切换每页条数调用
            this.selectPage = val;
            this.downListShow();
        },
        handleCurrentChange(val) {
        // 切换当前页数调用
            this.currentPage = val;
            this.downListShow();
        },
        // 侧边栏折叠
        collapseChage() {
            //this.$store.commit("hadndleCollapse", !this.collapse);
        },
        openUrl(url,id) {
           this.$http({
                url: "/api/download/task/addDownloadNum?id=" + id,
                method: "post",
                headers: {
                "Content-Type": "application/json",
                },
            }).then((data) => {
                if (data.code === 0) {
                    this.downListShow()
                    window.open(url);
                }
            });
            
        },
    },
    mounted() {
        if (document.body.clientWidth < 1500) {
            this.collapseChage();
        }
    }
};
</script>
<style scoped>
.header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 60px;
    font-size: 22px;
    color: #fff;
    background: #fff;
}
.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 60px;
}
.header .logo {
    float: left;
    width: 250px;
    line-height: 60px;
}
.header-right {
    float: right;
    padding-right: 10px;
}
.header-user-con {
    display: flex;
    height: 60px;
    align-items: center;
}
.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 110px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}
.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
}
.btn-bell .el-icon-bell {
    color: #333;
    position: relative;
    top: 3px;
    left: -6px;
}
.btn-bell img {
    position: relative;
    top: 2px;
    left: -5px;
}
.user-name {
    margin-left: 10px;
}
.user-avator {
    margin-left: 20px;
}
.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}
.el-dropdown-link {
    color: #0A2E6A;
    cursor: pointer;
    margin: 0 10px 0 5px;
}
.el-dropdown-menu__item {
    text-align: center;
    border-top: 0;
}
.gtgsh {
    font-size: 14px;
    font-weight: normal;
    color: #0A2E6A;
}
</style>
<style>
.el-icon-caret-bottom {
    font-size: 15px;
    color: rgba(149, 168, 190, 1);
}
</style>
